#include "RL_RS485_FSFX.h"


void RS485_FSFX::requestfs(){

  this->write(fs_request, sizeof(fs_request));

}

float RS485_FSFX::readfs(){
    delay(100);
    //this->write(fs_request, sizeof(fs_request));
    this->flush();
     if(this->available())  this->readBytes(fs_buf, 7);
    return (fs_buf[3]*256+fs_buf[4])*0.1;
    
}

int RS485_FSFX::readfs_level(){
    delay(100);
    float fs=readfs();
    
    if(fs<0.3) return 0;
    else if(fs>=0.3 && fs<1.6) return 1;
    else if(fs>=1.6 && fs<3.4) return 2;
    else if(fs>=3.4 && fs<5.5) return 3;
    else if(fs>=5.5 && fs<8.0) return 4;
    else if(fs>=8.0 && fs<10.8) return 5;
    else if(fs>=10.8 && fs<13.9) return 6;
    else if(fs>=13.9 && fs<17.2) return 7;
    else if(fs>=17.2 && fs<20.8) return 8;
    else if(fs>=20.8 && fs<24.5) return 9;
    else if(fs>=24.5 && fs<28.5) return 10;
    else if(fs>=28.5 && fs<32.7) return 11;
    else if(fs>=32.7 && fs<37.0) return 12;
    else if(fs>=37.0 && fs<41.5) return 13;
    else if(fs>=41.5 && fs<46.2) return 14;
    else if(fs>=46.2 && fs<51.0) return 15;
    else if(fs>=51.0 && fs<56.1) return 16;
    else if(fs>=56.1 ) return 17;

}

void RS485_FSFX::requestfx(){

  this->write(fx_request, sizeof(fx_request));

}

float RS485_FSFX::readfx(){
    delay(100);
    //this->write(fx_request, sizeof(fx_request));
    this->flush();
     if(this->available())  this->readBytes(fx_buf, 9);
    return (fx_buf[3]*256+fx_buf[4])*0.1;
    
}

bool RS485_FSFX::readfx_dir(int dir){
   delay(100);
   float fx=readfx();
   
  if(dir==0){
     if(fx<22.5 || (fx>=337.5 && fx<=360.0) )  return 1;
     else  return 0;
   }else if(dir==1){
     if(fx>=22.5 && fx<67.5)  return 1;
     else  return 0;
   }else if(dir==2){
     if(fx>=67.5 && fx<112.5)  return 1;
     else  return 0;
   }else if(dir==3){
     if(fx>=112.5 && fx<157.5)  return 1;
     else  return 0;
   }else if(dir==4){
     if(fx>=157.5 && fx<202.5)  return 1;
     else  return 0;
   }else if(dir==5){
     if(fx>=202.5 && fx<247.5)  return 1;
     else  return 0;
   }else if(dir==6){
     if(fx>=247.5 && fx<292.5)  return 1;
     else  return 0;
   }else if(dir==7){
     if(fx>=292.5 && fx<337.5)  return 1;
     else  return 0;
   }
}